<%-- 
    Document   : MessageIndex
    Created on : Dec 4, 2011, 3:56:28 PM
    Author     : hendrikde
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Inbox</title>
<script type="text/javascript">
    function message_load(txt) {
     document.forms[txt].submit();
    }
    
    function message_mouseover(obj){
        obj.style.backgroundColor="#6699FF";
    }
    
    function message_mouseout(obj){
        obj.style.backgroundColor="";
    }
    
    function delete_mouseover(obj){
        obj.style.backgroundColor="#6699FF";
    }
    
    function delete_mouseout(obj) {
        obj.style.backgroundColor="";
    }
    
    function delete_message(txt) {
        document.forms[txt].submit();
    }
    
    </script>
</head>


<body>
<table width="800" border="1">
    <tr>
        <td><img src="Logo.jpg" width="800" height="150" /></td>
    </tr>
  <tr>
    <th scope="col">Threads</th>
  </tr>
<%
String customer_id = session.getAttribute("UserID").toString();

DBClasses.GetConnection gc = new DBClasses.GetConnection();
java.sql.Connection conn = gc.getConnection();
    
try {
    java.sql.Statement stmt1 = conn.createStatement();
    String query1 = "SELECT C.Firstname, C.Lastname, M.Date, M.Subject, M.MessageID, C.CustomerID, M.MessageRead, M.Sender, M.DeletedBy "
            + "FROM Customer C, Messages M "
            + "WHERE (M.Sender='" + customer_id
            + "' AND C.CustomerID = M.Receiver) OR (M.Receiver='" + customer_id
            + "' AND C.CustomerID = M.Sender) "
            + "ORDER BY M.Date DESC";
    
    /*java.sql.Statement stmt2 = conn.createStatement();
    String query2 = "SELECT C.Firstname, C.Lastname, M.Date, M.Subject"
            + "FROM Customer C, Message M"
            + "WHERE M.Receiver='" + customer_id + "' AND C.CusomerID=M.Sender";*/
    
    java.sql.ResultSet rs1 = stmt1.executeQuery(query1);
    //java.sql.ResultSet rs2 = stmt2.executeQuery(query2);
    int i = 0;
    while(rs1.next()) {
        int read=Integer.parseInt(rs1.getString(7));
        int cur_customer=Integer.parseInt(customer_id);
        int sender=Integer.parseInt(rs1.getString(8));
        int deleted = Integer.parseInt(rs1.getString(9));
        int is_sender;
        if(cur_customer==sender)
            is_sender=1;
        else
            is_sender=0;
        if (deleted != cur_customer) {
        %>
    <tr>
    <td><table width="800" border="1">
      <tr>
          
          <td width="20" onmouseover ="delete_mouseover(this)" onmouseout="delete_mouseout(this)" onclick="delete_message('<%="delete" + i %>')">X
            <form name="<%="delete"+i%>" action="MessageDelete.jsp" method="post">
                <input name="MESS" type="hidden" value="<%=rs1.getString(5)%>" /></form></td>
                
          <td width="400">
              <label name="Firstname"><%=rs1.getString(1) %></label>
              <label name="Lastname" ><%=rs1.getString(2) %></label>
              <label name="Date" ><%=rs1.getString(3) %></label>
          </td>
        <td onmouseover="message_mouseover(this)" onmouseout="message_mouseout(this)" onclick="message_load('<%="message" + i %>')">
            <form name="<%="message" + i%>" action="MessageThread.jsp" method="post">
                <input name="MESS" type="hidden" value="<%=rs1.getString(5)%>"/>
                <input name="PERS" type="hidden" value="<%=rs1.getString(6)%>" />
                <input name="SEND" type="hidden" value="<%=is_sender%>"/>
                <%
                if (read>0 || is_sender > 0){ %>
            <label name="messagesubject"><%=rs1.getString(4) %></label>
            <%;}
              else  {%>
             <label name="messagesubject" style="color:red"><%=rs1.getString(4) %></label>
             <%;}%>
            </form></td>
      </tr>
    </table></td>
  </tr>
        <%;}
        i++;
    }
    
}
catch (Exception e) {
    e.printStackTrace();
       }
finally
    {
        try{
            conn.close();
        }
        catch(Exception ee)
                               {
            ee.printStackTrace();
        }
    }

%>


</table>
<input type="button" value="Compose New Message" onclick="window.location='NewMessage.jsp'" />
<input type="button" value="Return" onclick="window.location='gohome.jsp'"/>
</body>
</html>